package nimbus.binding.utils
{
	import flash.utils.Dictionary;
	import flash.utils.Proxy;
	import flash.utils.flash_proxy;
	
	import mx.binding.utils.ChangeWatcher;
	
	
	public dynamic class BindProxy extends Proxy
	{
		public var changeWatcher:ChangeWatcher;
		
		private var setter:Boolean;
		private var dictionary:Dictionary;
		
		public function BindProxy(item:Object, setter:Boolean = false)
		{
			dictionary = new Dictionary(true);
			dictionary[item] = true;
			this.setter = setter;
		}
		
		flash_proxy override function setProperty(name:*, value:*):void
		{
			var item:Object = getItem();
			if(item == null) return;
			
			if(!setter)
				item[name] = value;
			else
				item[name](value);
		}
		
		public function getItem():*
		{
			for(var i:* in dictionary)
			{
				return i;
			}
			changeWatcher.setHandler(null);
			changeWatcher.unwatch();
		}
		
		public function toString():String
		{
			return "[object BindProxy]";
		}
		
	}
}
